<template>
  <div class="wrapper">
    <div style="height: 60px; line-height: 60px; font-size: 20px; padding-left: 50px;color: white;
      background-color: rgba(0,0,0,0.2)">管理系统</div>
    <div style="margin: 50px auto; background-color: #fff; width: 420px; height: 400px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注 册</b></div>
      <el-form :model="user" :rules="rules" ref="userForm" >
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" size="medium" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" size="medium" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input placeholder="请确认密码" size="medium" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item prop="email">
        <el-input placeholder="请输入邮箱" size="medium" prefix-icon="el-icon-message" v-model="user.email"></el-input>
        </el-form-item>
     
          <el-form-item prop="code">
          <el-input style="width: 200px;"  placeholder="请确认验证码" size="medium" prefix-icon="el-icon-position"  v-model="user.code"></el-input>
          <el-button type="primary" size="medium" :disabled="disable" @click="sendVerificationCode" style="float: right;">
            {{buttonName }}
          </el-button>
        </el-form-item>
      
  
    

        <el-form-item style="margin: 5px 0; text-align: right">
          <el-button type="warning" size="small"  autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
          <el-button type="primary" size="small"  autocomplete="off" @click="login">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      user: {},
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 8, message: '特殊字符至少八位', trigger: 'blur' },
          {pattern:/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[!#@*&.])[a-zA-Z\d!#@*&.]*$/,message:'必须包含数字，拼英，特殊字符至少八位',trigger:'blur'}
        ],
        confirmPassword: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 8, message: '特殊字符至少八位', trigger: 'blur' },
          {pattern:/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[!#@*&.])[a-zA-Z\d!#@*&.]*$/,message:'必须包含数字，拼英，特殊字符至少八位',trigger:'blur'}
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
          {pattern:/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/,message:'无效邮箱',trigger:'blur'}
        ],
        
        // captcha: [
        //   { required: true, message: '请输入验证码', trigger: 'blur' },
        //   { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        // ],
        //code:[
          //{ required:true,message:  '验证码不能为空', trigger:'blur'},
          //{ min:4, max: 4,message:'验证码为4位数字',trigger:'blur'}
       // ]
      },
      buttonName:"获取验证码",
      count:60,
      disable:false,
    }
  },
  methods: {
    login() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
          if (this.user.password !== this.user.confirmPassword) {
            this.$message.error("两次输入的密码不一致")
            return false
          }
          var passwordreg = "/(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,12}/"
          // var isValid = passwordreg.test(this.user.password );
          // if(isValid != true){
          //   this.$message.error("密码必须是大写字母,小写字母,数字,特殊字符组成,长度为至少8位!");
          //   return;
          // }
          const valid = (rule, value, callback) =>{
	      if (!passwordreg.test(this.user.password)) {
	        this.$message.error("用户密码必须包含大写字母、小写字母、数字和特殊符号")
	      } 
        return false
	    }
          this.request.post("/user/register", this.user).then(res => {
            if(res.code === '200') {
              this.$message.success("注册成功")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });
    },
    sendVerificationCode() {
      this.request.post("/user/registerCode", this.user).then(res => {
        if(res.code === '200') {
          this.$message.success("发送成功")
        } else {
          this.$message.error(res.msg)
        }
      })
      var timer=setInterval(()=>{
        if(this.count<1){
            this.disable=false;
            this.buttonName="获取验证码";
            this.count=60;
            clearInterval(timer);
        }else{
          this.disable=true;
          this.buttonName=this.count--+"s后重发"
        }
      },1000)
    }
  }
}
</script>

<style>
  .wrapper {
    height: 100vh;
    background-image: linear-gradient(to bottom right, #4169E1 , 	#87CEFA);
    overflow: hidden;
  }
  
  
</style>
